
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px" v-loading="loading">
              <el-form-item label="所属建筑" prop="project_id">
              <el-select v-model.trim="data.project_id" placeholder="请选择所属建筑" clearable  class="filter-item" >
                <el-option  v-for="(project,index) in project_list" :key="index" :label="project.title" :value="project.id"></el-option>
              </el-select>
              </el-form-item>
              <el-form-item label="建筑名称" prop="title">
                  <el-input v-model.trim="data.title" placeholder="请填写建筑名称" />
              </el-form-item>
  
          <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
          </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {detail,edit} from '@/api/projectbuilding'
import { getToken } from '@/utils/auth'



export default {
  name: 'projectbuildingEdit',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    project_list: {
      type: Array,
      default: () => {
        return []
      }
    },
  },
  data() {
    return {
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
        rules: {
            project_id: [ 
                { required: true, message: "请输入所属建筑", trigger: "blur" }
            ], 

            title: [ 
                { required: true, message: "请输入建筑名称", trigger: "blur" }
            ], 

      },
      data: {
            project_id:'',
            title:'',

         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {
    
    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
            
          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit({
          id: this.data.id,
                        project_id:this.data.project_id,
            title:this.data.title,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
